#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_random.h"

#include "ssd1306.h"
//#include "font8x8_basic.h"

/*
 You have to set this config value with menuconfig
 CONFIG_INTERFACE

 for i2c
 CONFIG_MODEL
 CONFIG_SDA_GPIO
 CONFIG_SCL_GPIO
 CONFIG_RESET_GPIO

 for SPI
 CONFIG_CS_GPIO
 CONFIG_DC_GPIO
 CONFIG_RESET_GPIO
*/

#define TAG "SSD1306"

uint8_t highway_width1[] = {
// 'highway_width1', 128x64px
0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 
0xff, 0x1c, 0x00, 0x02, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x38, 0xff, 
0xfe, 0x5c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0x80, 0x00, 0x3a, 0x7f, 
0xfc, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0x3f, 
0xf9, 0xdc, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x80, 0x00, 0x40, 0x00, 0xc0, 0x00, 0x3b, 0x9f, 
0xf3, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xcf, 
0xe7, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xe7, 
0xcf, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xf3, 
0x9f, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xf9, 
0x3f, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfc, 
0x7f, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfe, 
0xff, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xff, 
0xff, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0xff, 
0xfe, 0x5c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3a, 0x7f, 
0xfc, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0x3f, 
0xf9, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0x9f, 
0xf3, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xcf, 
0xe7, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xe7, 
0xcf, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf3, 
0x9f, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf9, 
0x3f, 0xdc, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x80, 0x00, 0x40, 0x00, 0x40, 0x00, 0x3b, 0xfc, 
0x7f, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfe, 
0xff, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xff, 
0xff, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0xff, 
0xfe, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x7f, 
0xfc, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x3f, 
0xf9, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x9f, 
0xf3, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xcf, 
0xe7, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xe7, 
0xcf, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf3, 
0x9f, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf9, 
0x3f, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xfc, 
0x7f, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xfe, 
0xff, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xff, 
0xff, 0x1c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x38, 0xff, 
0xfe, 0x5c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3a, 0x7f, 
0xfc, 0xdc, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x80, 0x00, 0x40, 0x00, 0x40, 0x00, 0x3b, 0x3f, 
0xf9, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x9f, 
0xf3, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xcf, 
0xe7, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xe7, 
0xcf, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xf3, 
0x9f, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xf9, 
0x3f, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfc, 
0x7f, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfe, 
0xff, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xff, 
0xff, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x38, 0xff, 
0xff, 0x1c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3a, 0x7f, 
0xfe, 0x5c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0x3f, 
0xfc, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0x9f, 
0xf9, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xcf, 
0xf3, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xe7, 
0xe7, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf3, 
0xcf, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xf9, 
0x9f, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfc, 
0x3f, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfe, 
0x7f, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0xff, 
0xff, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0xff, 
0xff, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x7f, 
0xfe, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x3f, 
0xfc, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x9f, 
0xf9, 0xdc, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x80, 0x00, 0x40, 0x00, 0x40, 0x00, 0x3b, 0xcf, 
0xf3, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xe7, 
0xe7, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf3, 
0xcf, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf9
};

uint8_t highway_width2[] = {
// 'highway_width2', 128x64px
0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 
0xff, 0x9c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x39, 0xff, 
0xff, 0x5c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0x80, 0x00, 0x3a, 0xff, 
0xfe, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0x7f, 
0xfd, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xbf, 
0xfb, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xdf, 
0xf7, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xef, 
0xef, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xf7, 
0xdf, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfb, 
0xbf, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfd, 
0xff, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xff, 
0xff, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xff, 
0xff, 0x9c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0xff, 
0xff, 0x5c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3a, 0xff, 
0xfe, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0x7f, 
0xfd, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xbf, 
0xfb, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xdf, 
0xf7, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xe7, 
0xef, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf3, 
0xdf, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf9, 
0xbf, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xfc, 
0xff, 0xdc, 0x00, 0x01, 0xfc, 0x00, 0xfe, 0x00, 0x7f, 0x00, 0x3f, 0x80, 0x3f, 0x80, 0x3b, 0xfe, 
0xff, 0xdc, 0x00, 0x01, 0xfc, 0x00, 0xfe, 0x00, 0x7f, 0x00, 0x3f, 0x80, 0x3f, 0x80, 0x3b, 0xff, 
0xff, 0x9c, 0x00, 0x01, 0xfc, 0x00, 0xfe, 0x00, 0x7f, 0x00, 0x3f, 0x80, 0x3f, 0x80, 0x39, 0xff, 
0xff, 0x5c, 0x00, 0x01, 0xfc, 0x00, 0xfe, 0x00, 0x7f, 0x00, 0x3f, 0x80, 0x3f, 0x80, 0x3a, 0xff, 
0xfe, 0xdc, 0x00, 0x01, 0xfc, 0x00, 0xfe, 0x00, 0x7f, 0x00, 0x3f, 0x80, 0x3f, 0x80, 0x3b, 0x7f, 
0xfd, 0xdc, 0x00, 0x01, 0xfc, 0x00, 0xfe, 0x00, 0x7f, 0x00, 0x3f, 0x80, 0x3f, 0x80, 0x3b, 0x9f, 
0xfb, 0xdc, 0x00, 0x01, 0xfc, 0x00, 0xfe, 0x00, 0x7f, 0x00, 0x3f, 0x80, 0x3f, 0x80, 0x3b, 0xdf, 
0xf7, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x40, 0x00, 0x3b, 0xe7, 
0xef, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf3, 
0xdf, 0xdc, 0x03, 0xfe, 0x01, 0xff, 0x00, 0xff, 0x80, 0x7f, 0xc0, 0x7f, 0xc0, 0x00, 0x3b, 0xf9, 
0xbf, 0xdc, 0x03, 0xfe, 0x01, 0xff, 0x00, 0xff, 0x80, 0x7f, 0xc0, 0x7f, 0xc0, 0x00, 0x3b, 0xfd, 
0xff, 0xdc, 0x03, 0xfe, 0x01, 0xff, 0x00, 0xff, 0x80, 0x7f, 0xc0, 0x7f, 0xc0, 0x00, 0x3b, 0xfe, 
0xff, 0xdc, 0x03, 0xfe, 0x01, 0xff, 0x00, 0xff, 0x80, 0x7f, 0xc0, 0x7f, 0xc0, 0x00, 0x3b, 0xff, 
0xff, 0x9c, 0x03, 0xfe, 0x01, 0xff, 0x00, 0xff, 0x80, 0x7f, 0xc0, 0x7f, 0xc0, 0x00, 0x39, 0xff, 
0xff, 0x5c, 0x03, 0xfe, 0x01, 0xff, 0x00, 0xff, 0x80, 0x7f, 0xc0, 0x7f, 0xc0, 0x00, 0x3a, 0xff, 
0xfe, 0xdc, 0x03, 0xfe, 0x01, 0xff, 0x00, 0xff, 0x80, 0x7f, 0xc0, 0x7f, 0xc0, 0x00, 0x3b, 0x7f, 
0xfd, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x9f, 
0xfb, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xdf, 
0xf7, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xe7, 
0xef, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xf3, 
0xdf, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xf9, 
0xbf, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfd, 
0xff, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfe, 
0xff, 0xdc, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x80, 0x00, 0x40, 0x00, 0x40, 0x00, 0x3b, 0xff, 
0xff, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x39, 0xff, 
0xff, 0x9c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3a, 0xff, 
0xff, 0x5c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0x7f, 
0xfe, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0x9f, 
0xfd, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xdf, 
0xfb, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xe7, 
0xf7, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf3, 
0xef, 0xdc, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x80, 0x00, 0x40, 0x00, 0x40, 0x00, 0x3b, 0xf9, 
0xdf, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfd, 
0xbf, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xfe, 
0xff, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0xff, 
0xff, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0xff, 
0xff, 0x9c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x7f, 
0xff, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x3f, 
0xfe, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x9f, 
0xfd, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xdf, 
0xfb, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xe7, 
0xf7, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf3, 
0xef, 0xdc, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x3b, 0xf9
};

uint8_t vehicle1[] = {
// 'vehicle1', 24x48px
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
0x83, 0xff, 0xfc, 0x7c, 0xff, 0xf1, 0xff, 0x3f, 0xf3, 0xff, 0x9f, 0xef, 0xff, 0xdf, 0xef, 0xff, 
0xef, 0xef, 0xff, 0xef, 0xef, 0xff, 0xef, 0xef, 0xff, 0xef, 0xef, 0xff, 0xef, 0xef, 0xff, 0xef, 
0xee, 0x00, 0xff, 0xe8, 0x00, 0x6f, 0xe8, 0x00, 0x2f, 0xe8, 0x00, 0x6f, 0xc8, 0x00, 0x47, 0xe0, 
0x00, 0x4f, 0xe4, 0x00, 0x5f, 0xe7, 0xc7, 0x8f, 0xe0, 0x00, 0x2f, 0xe1, 0xc7, 0x2f, 0xe3, 0xc7, 
0x0f, 0xe3, 0xc7, 0x2f, 0xe1, 0xc7, 0x2f, 0xe3, 0xc7, 0x2f, 0xe3, 0xc7, 0x0f, 0xe1, 0xc7, 0x2f, 
0xe8, 0x00, 0x6f, 0xef, 0x83, 0xef, 0xe9, 0xfe, 0x2f, 0xec, 0x00, 0x6f, 0xef, 0x83, 0xef, 0xef, 
0xff, 0xff, 0xef, 0xff, 0xef, 0xef, 0xff, 0xdf, 0xef, 0xff, 0xdf, 0xf7, 0xff, 0xdf, 0xf3, 0xff, 
0xbf, 0xfc, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

uint8_t vehicle2[] = {
// 'vehicle2', 24x48px
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x7f, 0xfc, 
0xff, 0x9f, 0xfb, 0xff, 0xef, 0xfb, 0xff, 0xef, 0xfb, 0xff, 0xff, 0xf7, 0xff, 0xf7, 0xf7, 0xff, 
0xf7, 0xf7, 0xff, 0xf7, 0xf7, 0xff, 0xf7, 0xf7, 0xff, 0xf7, 0xf7, 0xff, 0xf7, 0xf7, 0x00, 0x77, 
0xf4, 0x00, 0x17, 0xf4, 0x00, 0x17, 0xe2, 0x00, 0x23, 0xf0, 0x00, 0x07, 0xf1, 0xff, 0xc7, 0xf5, 
0xff, 0xc7, 0xf1, 0xff, 0xc7, 0xf1, 0xff, 0xd7, 0xf5, 0xff, 0xd7, 0xf1, 0xff, 0xd7, 0xf1, 0xff, 
0xd7, 0xf5, 0xff, 0xd7, 0xf1, 0xff, 0xd7, 0xf1, 0xff, 0xd7, 0xf1, 0xff, 0xd7, 0xf5, 0xff, 0xd7, 
0xf5, 0xff, 0xd7, 0xf5, 0x00, 0x47, 0xf6, 0x00, 0x37, 0xf6, 0x00, 0x37, 0xf6, 0x00, 0x37, 0xf6, 
0x00, 0x37, 0xff, 0x00, 0x77, 0xfb, 0xff, 0xef, 0xfb, 0xff, 0xef, 0xfb, 0xff, 0xef, 0xfb, 0xff, 
0xef, 0xfc, 0x7f, 0x1f, 0xff, 0xc1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

uint8_t vehicle3[] = {
// 'vehicle3', 24x48px
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x7f, 0xfe, 0x3f, 0x3f, 0xf8, 
0xff, 0xcf, 0xfb, 0xff, 0xe7, 0xf7, 0xff, 0xf7, 0xf7, 0xff, 0xf3, 0xe7, 0xff, 0xf3, 0xe7, 0xff, 
0xfb, 0xe7, 0xff, 0xfb, 0xe7, 0xff, 0xfb, 0xe6, 0x00, 0x3b, 0xe4, 0x00, 0x0b, 0xe0, 0x00, 0x0b, 
0xe4, 0x00, 0x0b, 0xe0, 0x00, 0x01, 0xc0, 0x00, 0x01, 0xc0, 0x00, 0x01, 0xe0, 0x00, 0x03, 0xe1, 
0xff, 0xc3, 0xe1, 0xff, 0xc3, 0xe1, 0xff, 0xc3, 0xe1, 0xff, 0xc3, 0xe1, 0xff, 0xc3, 0xe1, 0xff, 
0xc3, 0xe1, 0xff, 0xc3, 0xe1, 0xff, 0xc3, 0xe1, 0xff, 0xc3, 0xe1, 0xff, 0xc3, 0xe1, 0xff, 0xc3, 
0xe1, 0xff, 0xc3, 0xe1, 0xff, 0xcb, 0xe1, 0xff, 0xcb, 0xed, 0xff, 0xfb, 0xe6, 0x00, 0x3b, 0xe6, 
0x00, 0x1b, 0xe4, 0x00, 0x1b, 0xe4, 0x00, 0x13, 0xf4, 0x00, 0x13, 0xf3, 0xc1, 0xf7, 0xf8, 0x3f, 
0x0f, 0xfe, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

#define WITH_VEHICLE 1

void app_main(void)
{
	SSD1306_t dev;

#if CONFIG_I2C_INTERFACE
	ESP_LOGI(TAG, "INTERFACE is i2c");
	ESP_LOGI(TAG, "CONFIG_SDA_GPIO=%d",CONFIG_SDA_GPIO);
	ESP_LOGI(TAG, "CONFIG_SCL_GPIO=%d",CONFIG_SCL_GPIO);
	ESP_LOGI(TAG, "CONFIG_RESET_GPIO=%d",CONFIG_RESET_GPIO);
	i2c_master_init(&dev, CONFIG_SDA_GPIO, CONFIG_SCL_GPIO, CONFIG_RESET_GPIO);
#endif // CONFIG_I2C_INTERFACE

#if CONFIG_SPI_INTERFACE
	ESP_LOGI(TAG, "INTERFACE is SPI");
	ESP_LOGI(TAG, "CONFIG_MOSI_GPIO=%d",CONFIG_MOSI_GPIO);
	ESP_LOGI(TAG, "CONFIG_SCLK_GPIO=%d",CONFIG_SCLK_GPIO);
	ESP_LOGI(TAG, "CONFIG_CS_GPIO=%d",CONFIG_CS_GPIO);
	ESP_LOGI(TAG, "CONFIG_DC_GPIO=%d",CONFIG_DC_GPIO);
	ESP_LOGI(TAG, "CONFIG_RESET_GPIO=%d",CONFIG_RESET_GPIO);
	spi_master_init(&dev, CONFIG_MOSI_GPIO, CONFIG_SCLK_GPIO, CONFIG_CS_GPIO, CONFIG_DC_GPIO, CONFIG_RESET_GPIO);
#endif // CONFIG_SPI_INTERFACE

#if CONFIG_FLIP
	dev._flip = true;
	ESP_LOGW(TAG, "Flip upside down");
#endif

#if CONFIG_SSD1306_128x64
	ESP_LOGI(TAG, "Panel is 128x64");
	ssd1306_init(&dev, 128, 64);
#endif // CONFIG_SSD1306_128x64
#if CONFIG_SSD1306_128x32
	ESP_LOGE(TAG, "Panel is 128x32. This demo cannot be run.");
	while(1) { vTaskDelay(1); }
#endif // CONFIG_SSD1306_128x32

	// Allocate memory for work frame
	uint8_t *wk = (uint8_t *)malloc(1024);
	if (wk == NULL) {
		ESP_LOGE(TAG, "malloc failed");
		while(1) { vTaskDelay(1); }
	}

	// Allocate memory for save frame
	uint8_t *save = (uint8_t *)malloc(1024);
	if (save == NULL) {
		ESP_LOGE(TAG, "malloc failed");
		while(1) { vTaskDelay(1); }
	}

	// Allocate memory for background frame
	uint8_t *buffer = (uint8_t *)malloc(1024*2);
	if (buffer == NULL) {
		ESP_LOGE(TAG, "malloc failed");
		while(1) { vTaskDelay(1); }
	}

	// Allocate memory for vehicle frame
	uint8_t *vehicle = (uint8_t *)malloc(1024*3);
	if (vehicle == NULL) {
		ESP_LOGE(TAG, "malloc failed");
		while(1) { vTaskDelay(1); }
	}

	ssd1306_contrast(&dev, 0xff);

	ssd1306_clear_screen(&dev, false);
	ssd1306_bitmaps(&dev, 0, 0, vehicle1, 24, 48, true);
	ssd1306_get_buffer(&dev, &vehicle[1024*0]);
	vTaskDelay(10);

	ssd1306_clear_screen(&dev, false);
	ssd1306_bitmaps(&dev, 0, 0, vehicle2, 24, 48, true);
	ssd1306_get_buffer(&dev, &vehicle[1024*1]);
	vTaskDelay(10);

	ssd1306_clear_screen(&dev, false);
	ssd1306_bitmaps(&dev, 0, 0, vehicle3, 24, 48, true);
	ssd1306_get_buffer(&dev, &vehicle[1024*2]);
	vTaskDelay(10);

	ssd1306_clear_screen(&dev, false);
	ssd1306_bitmaps(&dev, 0, 0, highway_width1, 128, 64, false);
	ssd1306_get_buffer(&dev, &buffer[1024*0]);
	vTaskDelay(10);

	ssd1306_clear_screen(&dev, false);
	ssd1306_bitmaps(&dev, 0, 0, highway_width2, 128, 64, false);
	ssd1306_get_buffer(&dev, &buffer[1024*1]);
	vTaskDelay(10);

	// Show initial background
	ssd1306_set_buffer(&dev, &buffer[1024*0]);
	ssd1306_show_buffer(&dev);
	vTaskDelay(10);

	int frameIndex=1;
	int page = 0;
	int vehicleHorizontalPosition[3] = {20, 52, 84};
	int vehicleVerticalPosition[3] = {-8, 1, -14};
	int vehicleVerticalMove[3] = {1, 0, 1};
	while(1) {
		ssd1306_wrap_arround(&dev, PAGE_SCROLL_DOWN, 0, 0, -1);

		// Get from internal buffer to local buffer
		// buffer is [8][128] 8 page 128 pixel
		ssd1306_get_buffer(&dev, wk);

		// Update buffer
		int frameOffset = frameIndex*1024;
		int dst_index = 0;
		int src_index = (7-page)*128+frameOffset;
		ESP_LOGD(TAG, "frameIndex=%d page=%d src_index=%d", frameIndex, page, src_index);
		for (int seg=0;seg<128;seg++) {
			wk[dst_index++] = buffer[src_index++];
		}

		// Set frame without vehicle
		ssd1306_set_buffer(&dev, wk);


#if WITH_VEHICLE
		// Get frame without vehicle
		ssd1306_get_buffer(&dev, save);

		// Set vehicle image
		for (int vehicleFrame=0;vehicleFrame<3;vehicleFrame++) {
			int startPage = vehicleVerticalPosition[vehicleFrame];
			int endPage = startPage+6;
			ESP_LOGD(TAG, "page=%d vehicleVerticalPosition1=%d startPage=%d endPage=%d",
				page, vehicleVerticalPosition[vehicleFrame], startPage, endPage);
			int vehicleIndex = vehicleFrame * 1024;
			int vehicleOffset = 0;
			for(int _page=startPage;_page<endPage;_page++) {
				for(int seg=0;seg<24;seg++) {
					int bufferIndex = _page*128+seg+vehicleHorizontalPosition[vehicleFrame];
					if (bufferIndex >= 0 && bufferIndex < 1023)
						wk[bufferIndex] = vehicle[vehicleIndex+vehicleOffset*128+seg];
				}
				vehicleOffset++;
			}
		}

		ssd1306_set_buffer(&dev, wk);
#endif

		// Show internal buffer
		ssd1306_show_buffer(&dev);

#if WITH_VEHICLE
		// Set frame without vehicle
		ssd1306_set_buffer(&dev, save);
#endif

		page++;
		if (page > 7) {
			page = 0;
			for (int vehicleFrame=0;vehicleFrame<3;vehicleFrame++) {
				vehicleVerticalPosition[vehicleFrame] = vehicleVerticalPosition[vehicleFrame] + vehicleVerticalMove[vehicleFrame];
				if (vehicleVerticalPosition[vehicleFrame] > 8) {
					vehicleVerticalPosition[vehicleFrame] = -8;
				}
			}
			frameIndex++;
			if (frameIndex > 1) frameIndex=0;
		}
	} // end while

}
